package com.hezhi.trafficcopsys.view.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import com.hezhi.framework.Constants;
import com.hezhi.framework.action.BaseAction;
import com.hezhi.framework.exception.APPException;
import com.hezhi.trafficcopsys.domain.User;
import com.hezhi.trafficcopsys.service.UserService;
import com.hezhi.trafficcopsys.service.impl.UserServiceImpl;
import com.hezhi.trafficcopsys.util.Constants4sys;

public class UserAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1687367066058467007L;
	
	//注入Service
	private UserService userService;

	public UserAction() {
		userService = new UserServiceImpl();
	}
	
	//form bean
	private User user;

	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	
	/**
	 * 添加用户
	 * @throws IOException
	 */
	public void doAddUser() throws IOException{
		try{
			//user.setPassword(MD5Util.getMd5(password));
			userService.addUser(user);
		} catch (Exception e){
			response.setCharacterEncoding(Constants4sys.ENCODING_UTF8);
			response.getWriter().print(e.getMessage());
		}
	}
	
	/**
	 * 编辑选中的用户
	 * @throws IOException
	 */
	public void doEditUser() throws IOException{
		try{
			//user.setPassword(MD5Util.getMd5(password));
			userService.editUser(user);
		} catch (APPException e){
			response.setCharacterEncoding(Constants4sys.ENCODING_UTF8);
			response.getWriter().print(e.getMessage());
		}
	}
	
	/**
	 * 删除选中的用户
	 * @throws IOException
	 */
	public void doDelUser() throws IOException{
		try {
			String username  = request.getParameter("username");
			String[] usernames = null;
			if( username != null ){
				if( username.contains(",")){
					usernames = username.split(",");
				} else {
					usernames = new String[1];
					usernames[0] = username;
				}
				userService.delUsers(usernames);
			}
		} catch (APPException e) {
			response.setCharacterEncoding(Constants.ENCODING_UTF8);
			response.getWriter().print(e.getMessage());
		}
	}

	/**
	 * 列出所有用户
	 * @throws IOException
	 */
	public void doLoadUsers() throws IOException {

		try {
			/**
			 * 获取前台数据请求
			 */
			// 页面大小
			int pageSize = Integer.parseInt(request
					.getParameter(Constants4sys.PAGE_SIZE));
			// 当前页
			int pageNo = Integer.parseInt(request
					.getParameter(Constants4sys.PAGE_NO));

			/**
			 * 调用查询方法
			 */
			List<User> listUsers = userService.listUserByPage(pageSize, pageNo);

			/**
			 * 封装Json数据
			 */
			int total = userService.getTotalSize(); // 符合查询的总条数
			if (listUsers == null) {
				// 注：如果从数据库查询不出数据，也必须封装一个空的json集合，不然页面就会报js错误
				listUsers = new ArrayList<User>();
			}
			JSONObject datas = new JSONObject();
			// 设置总共有多少条记录
			datas.put(Constants4sys.TOTAL, total);
			// 设置当前页的数据
			datas.put(Constants4sys.PAGE_SIZE, listUsers);

			response.setCharacterEncoding(Constants4sys.ENCODING_UTF8);
			response.getWriter().print(datas.toString());
		} catch (APPException e) {
			response.setCharacterEncoding(Constants.ENCODING_UTF8);
			response.getWriter().print(e.getMessage());
		}
	}
}
